package cn.orange.ch07_binarytree;

import cn.orange.TreeNode;

/**
 * LC98.验证二叉搜索树
 */
public class LC98 {
    private TreeNode prev = null;

    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean bL = isValidBST(root.left);
        if (prev != null) {
            if (prev.val >= root.val) {
                return false;
            }
        }
        prev = root;
        boolean bR = isValidBST(root.right);
        return bL && bR;
    }

    public static void main(String[] args) {
        LC98 alg = new LC98();
        TreeNode root1 = new TreeNode(5, new TreeNode(4), new TreeNode(6));
        root1.right.left = new TreeNode(3);
        root1.right.right = new TreeNode(7);
        System.out.println(alg.isValidBST(root1));
        TreeNode root2 = new TreeNode(2, new TreeNode(2), new TreeNode(2));
        System.out.println(alg.isValidBST(root2));
    }
}
